package ysq.xyg.host.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.ProProperties;
import ysq.xyg.host.dao.HostItemUpdateDao;
import ysq.xyg.host.model.dto.HostItemUpdateDto;
import ysq.xyg.host.model.po.HostItemSubscription;
import ysq.xyg.host.model.po.HostItemUpdate;
import ysq.xyg.host.model.vo.HostItemSubscriptionVo;
import ysq.xyg.host.model.vo.HostItemUpdateSearchVo;
import ysq.xyg.host.model.vo.HostItemUpdateVo;
import ysq.xyg.host.service.HostItemSubscriptionService;
import ysq.xyg.host.service.HostItemUpdateService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.wxapi.api.WxApiClient;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘动态service实现
 *
 * @author congcong tao
 * @date 2019-12-13 9:38:19
 */
@Service("hostItemUpdateService")
public class HostItemUpdateServiceImpl extends BaseServiceImpl<HostItemUpdate> implements HostItemUpdateService {

    @Autowired
    private HostItemUpdateDao hostItemUpdateDao;
    @Autowired
    private HostItemSubscriptionService hostItemSubscriptionService;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    public void setBaseDao(HostItemUpdateDao hostItemUpdateDao) {
        this.baseDao = hostItemUpdateDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemUpdateVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 9:38:19
     */
    @Override
    public ReturnEntity page(HostItemUpdateVo hostItemUpdateVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostItemUpdate> list = acceptList(hostItemUpdateVo);
        PageInfo<HostItemUpdate> page = new PageInfo<HostItemUpdate>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemUpdateVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 9:38:19
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemUpdateVo hostItemUpdateVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItemUpdate hostItemUpdate;
        if (StrUtil.isEmpty(hostItemUpdateVo.getId())) {
            hostItemUpdate = new HostItemUpdate(currentUser);
            isUpate = false;
        } else {
            hostItemUpdate = selectByPrimaryKey(hostItemUpdateVo.getId());
            if (ObjectUtil.isNull(hostItemUpdate)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItemUpdate.setUpdateTime(new Date());
            hostItemUpdate.setUpdateUser(currentUser.getId());
        }
        hostItemUpdate.setItemId(hostItemUpdateVo.getItemId());
        hostItemUpdate.setItemName(hostItemUpdateVo.getItemName());
        hostItemUpdate.setCityId(hostItemUpdateVo.getCityId());
        hostItemUpdate.setName(hostItemUpdateVo.getName());
        hostItemUpdate.setDescription(hostItemUpdateVo.getDescription());
        hostItemUpdate.setContent(hostItemUpdateVo.getContent());
        hostItemUpdate.setSourceType(hostItemUpdateVo.getSourceType());
        int num;
        if (isUpate) {
            num = updateByPrimaryKey(hostItemUpdate);
        } else {
            num = insert(hostItemUpdate);
            // 新增时发送通知
            String sendSmsFlag = ProProperties.getInstance().getSendSmsFlag();
            if (StringUtils.equals("true", sendSmsFlag)) {
                taskExecutor.execute(() -> {
                    // 查询到相关数据
                    HostItemSubscriptionVo hostItemSubscriptionVo = new HostItemSubscriptionVo();
                    hostItemSubscriptionVo.setItemId(hostItemUpdate.getItemId());
                    List<HostItemSubscription> hostItemSubscriptions = hostItemSubscriptionService.acceptList(hostItemSubscriptionVo);
                    if (CollUtil.isNotEmpty(hostItemSubscriptions)) {
                        for (HostItemSubscription hostItemSubscription : hostItemSubscriptions) {
                            // 发送
                            WxApiClient.sendSubscribeNote(hostItemUpdate.getItemId(), hostItemSubscription.getOpenId(),
                                    hostItemUpdate.getItemName(), DateUtil.formatDateTime(hostItemUpdate.getCreateTime()));
                        }
                    }
                });
            }
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 9:38:19
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItemUpdate hostItemUpdate = selectByPrimaryKey(id);
        if (hostItemUpdate != null) {
            hostItemUpdate.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItemUpdate.setUpdateTime(new Date());
            hostItemUpdate.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostItemUpdate);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemUpdateVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-13 9:38:19
     */
    @Override
    public List<HostItemUpdate> acceptList(HostItemUpdateVo hostItemUpdateVo){
        Example example = new Example(HostItemUpdate.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemUpdateVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemUpdateVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostItemUpdateVo.getItemId())) {
            criteria.andEqualTo("itemId", hostItemUpdateVo.getItemId());
        }
        example.setOrderByClause(" update_time desc");
        return hostItemUpdateDao.selectByExample(example);
    }

    @Override
    public List<HostItemUpdateDto> selectHostItemUpdateList(String cityId, int limit) {
        HostItemUpdateSearchVo searchVo = new HostItemUpdateSearchVo();
        searchVo.setCityId(cityId);
        searchVo.setLimit(limit);
        return hostItemUpdateDao.selectHostItemUpdateList(searchVo);
    }

    @Override
    public List<HostItemUpdateDto> selectHostItemUpdateList(String itemId, String cityId, int limit) {
        HostItemUpdateSearchVo searchVo = new HostItemUpdateSearchVo();
        searchVo.setItemId(itemId);
        searchVo.setCityId(cityId);
        searchVo.setLimit(limit);
        return hostItemUpdateDao.selectHostItemUpdateList(searchVo);
    }
}
